package com.scm.controller;

/**
 * @author CHL
 * @ClassName: ProductController
 * @Description: TODO
 * @date 2022/3/8 14:32
 * @Version 1.0
 */
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scm.dao.ProductMapper;
import com.scm.entity.Product;
import com.scm.entity.response.ResponseMessage;
import com.scm.util.StringUtil;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/main/sell"})
public class ProductController
{
    @Autowired
    private ProductMapper productMapper;
    private ResponseMessage rm = new ResponseMessage();

    public boolean checkProduct(Product product)
    {
        if (product == null)
        {
            this.rm.setMessage("产品信息不能为空");
            return false;
        }
        String productCode = product.getProductCode();
        if (StringUtil.isEmpty(productCode))
        {
            this.rm.setMessage("产品编号不能为空productCode");
            return false;
        }
        if (!productCode.matches("^[a-zA-Z0-9]{4,20}$"))
        {
            this.rm.setMessage("产品编号参数为4-20位数字字母");
            return false;
        }
        String name = product.getName();
        if (StringUtil.isEmpty(name))
        {
            this.rm.setMessage("名称name不能为空");
            return false;
        }
        if (name.length() > 100)
        {
            this.rm.setMessage("名称name长度不超过100");
            return false;
        }
        String unitName = product.getUnitName();
        if (StringUtil.isEmpty(unitName))
        {
            this.rm.setMessage("数量单位不能为空");
            return false;
        }
        Double price = product.getPrice();
        if (price == null)
        {
            this.rm.setMessage("销售价格不能为空");
            return false;
        }
        if (price.doubleValue() <= 0.0D)
        {
            this.rm.setMessage("销售价格必须为正数");
            return false;
        }
        Integer categoryId = product.getCategoryId();
        if (StringUtil.isEmpty(categoryId))
        {
            this.rm.setMessage("分类编号不能为空");
            return false;
        }
        return true;
    }

    @RequestMapping({"/product/add"})
    public ResponseMessage productAdd(Product product)
    {
        if (!checkProduct(product))
        {
            this.rm.setCode(Integer.valueOf(3));
            return this.rm;
        }
        this.productMapper.addProduct(product);
        this.rm.setCode(Integer.valueOf(2));
        this.rm.setMessage("添加产品成功");
        return this.rm;
    }

    @RequestMapping({"/product/delete"})
    public ResponseMessage delete(String productCode)
    {
        if (productCode == null)
        {
            this.rm.setCode(Integer.valueOf(3));
            this.rm.setMessage("缺少参数");
        }
        else
        {
            int n = this.productMapper.deleteProduct(productCode);
            if (n == 0)
            {
                this.rm.setCode(Integer.valueOf(4));
                this.rm.setMessage("修改失败");
            }
            else
            {
                this.rm.setCode(Integer.valueOf(2));
                this.rm.setMessage("删除成功");
                this.rm.setData(this.productMapper.select(null));
            }
        }
        return this.rm;
    }

    @RequestMapping({"/product/update"})
    public ResponseMessage update(Product product)
    {
        if (!checkProduct(product))
        {
            this.rm.setCode(Integer.valueOf(3));
            return this.rm;
        }
        int n = this.productMapper.updateProduct(product);
        if (n == 0)
        {
            this.rm.setCode(Integer.valueOf(4));
            this.rm.setMessage("修改失败");
        }
        else
        {
            this.rm.setCode(Integer.valueOf(2));
            this.rm.setMessage("修改成功");
        }
        return this.rm;
    }

    @RequestMapping({"/product/show"})
    public PageInfo<Product> select(Product product, Integer page)
    {
        PageHelper.startPage(page == null ? 1 : page.intValue(), 10);
        List<Product> products = this.productMapper.select(product);
        PageInfo<Product> info = new PageInfo(products);
        return info;
    }

    @RequestMapping({"/product/all"})
    public List<Product> selectAll()
    {
        List<Product> products = this.productMapper.select(null);
        return products;
    }
}

